package com.mygdx.game.Scenes;
//asdasdasdasdasdasdasdasdasdasdasdasd
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.Screens.PlayScreen;

public class ActiveCharacter extends Actor {
    public static boolean Collision = false;
    public boolean move = false;
    public static float x;
    public static float y;
    public static float width;
    public static float heigth;
    private String image;
    public static float statetime = 0;

    TextureRegion[] walkFrame;
    TextureRegion[] walkFrame2;
    TextureRegion currentFrame;
    TextureRegion currentFrame2;
    Animation aniRight;
    Animation aniRight2;


    public ActiveCharacter(String image, float x, float y){
        this.x = x;
        this.y = y;
        this.image=image;
        this.show();
    }
    public Float getx(){
        return x;
    }
    public Float gety(){
        return y;
    }

    public void show() {
            Texture[] temp = new Texture[4];
            walkFrame = new TextureRegion[4];
            for (int i = 0; i < 4; i++) {
                temp[i] = new Texture(Gdx.files.internal(image + " (" + (4 - i) + ").png"));
                width = temp[i].getWidth();
                heigth = temp[i].getHeight();
                walkFrame[i] = new TextureRegion(temp[i], 0, 0, 100, 100);

        }
        Texture[] temp2 = new Texture[4];
        walkFrame2 = new TextureRegion[4];
        for (int i = 0; i < 4; i++) {
            temp2[i] = new Texture(Gdx.files.internal(image + " (" + (4 - i) + ").png"));
            width = temp2[i].getWidth();
            heigth = temp2[i].getHeight();
            walkFrame2[i] = new TextureRegion(temp[i], 0, 0, 100, 100);
            //反方向asdasd
            walkFrame2[i].flip(true,false);
        }
            aniRight = new Animation(0.1f, walkFrame);
            aniRight2 = new Animation(0.1f, walkFrame2);

    }

    public void update(){
//        x=PlayScreen.mario.getX()+4;
//        y=PlayScreen.mario.getY()+2;

        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            x+=2f;
            System.out.println("RIGHT");
        }
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            x-=2f;
            System.out.println("LEFT");
        }
        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            y+=2f;
            System.out.println("UP");
        }
        if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
            y-=2f;
            System.out.println("DOWN");
        }
//        //左边界-100 右边界1550 上边界700 下边界-100
//        if (x<-100){
//            x=-100;
//        }
//        if (x> MyGdxGame.V_WIDTH)
//        {
//            x=MyGdxGame.V_WIDTH;
//        }
//        if (y<-100)
//        {
//            y=-100;
//        }
//        if(y>MyGdxGame.V_HEIGHT){
//            y=MyGdxGame.V_HEIGHT;
//        }

    }

    public void RightSide(){
        currentFrame2 = (TextureRegion) aniRight2.getKeyFrame(statetime, true);

    }
    public void LeftSide(){
        currentFrame = (TextureRegion) aniRight.getKeyFrame(statetime,true);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        statetime += Gdx.graphics.getDeltaTime();
        if(move==true) {
            update();
        }
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            this.LeftSide();
            batch.draw(currentFrame, x, y);
        }
        else if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            this.RightSide();
            batch.draw(currentFrame2, x, y);
        }
        else if(Gdx.input.isKeyPressed(Input.Keys.UP)) {
            this.RightSide();
            batch.draw(currentFrame2, x, y);
        }
        else if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            this.LeftSide();
            batch.draw(currentFrame, x, y);
        }

        else {
            this.LeftSide();
            batch.draw(currentFrame, x, y);
//            batch.draw(walkFrame[0], x, y);
       }


    }
    @Override
    public void act(float delta) {
        super.act(delta);
    }
}
